#include "rkmedia_function_register.h"

extern containerInfo *above_container_func;

int rkmedia_function_achieve(int high_protocol, char *high_url_address, int low_protocol, char *low_url_address)
{
    int ret = -1;

#if 1
    ffmpegInfo *high_ffmpeg_config = new ffmpegInfo;
    assert(high_ffmpeg_config);
    memset(high_ffmpeg_config->nAddr, 0, sizeof(high_ffmpeg_config->nAddr)); // 结构体里面的数组使用之前一定要进行初始化，不然可能会有多余的内容！！！
    cout << "Get high_ffmpeg_config ..." << endl;

    high_ffmpeg_config->index = 0;
    high_ffmpeg_config->protocol = high_protocol;
    // high_ffmpeg_config->protocol = HIGH_PROTOCOL;
    high_ffmpeg_config->video_codec = AV_CODEC_ID_H264;
    high_ffmpeg_config->audio_codec = AV_CODEC_ID_AAC;
    high_ffmpeg_config->u32Width = PIX_WIDTH_SIZE;
    high_ffmpeg_config->u32Height = PIX_HEIGHT_SIZE;
    memcpy(high_ffmpeg_config->nAddr, high_url_address, strlen(high_url_address));
    // memcpy(high_ffmpeg_config->nAddr, HIGH_URL_ADDR, strlen(HIGH_URL_ADDR));
    ret = init_rkmedia_ffmpeg_context(high_ffmpeg_config);
    if (ret)
    {
        cout << "init_rkmedia_ffmpeg_context high failed ..." << endl;
        return -1;
    }
    else
    {
        cout << "init_rkmedia_ffmpeg_context high success ..." << endl;
    }
#endif

#if 0
    ffmpegInfo *low_ffmpeg_config = new ffmpegInfo;
    assert(low_ffmpeg_config);
    memset(low_ffmpeg_config->nAddr, 0, sizeof(low_ffmpeg_config->nAddr));
    cout << "Get low_ffmpeg_config ..." << endl;

    low_ffmpeg_config->index = 1;
    low_ffmpeg_config->protocol = low_protocol;
    // low_ffmpeg_config->protocol = LOW_PROTOCOL;
    low_ffmpeg_config->video_codec = AV_CODEC_ID_H264;
    //low_ffmpeg_config->video_codec = AV_CODEC_ID_AAC;
    low_ffmpeg_config->u32Width = 1280;
    low_ffmpeg_config->u32Height = 720;
    memcpy(low_ffmpeg_config->nAddr, low_url_address, strlen(low_url_address));
    // memcpy(low_ffmpeg_config->nAddr, LOW_URL_ADDR, strlen(LOW_URL_ADDR));
    ret = init_rkmedia_ffmpeg_context(low_ffmpeg_config);
    if (ret)
    {
        cout << "init_rkmedia_ffmpeg_context low failed ..." << endl;
        return -1;
    }
    else
    {
        cout << "init_rkmedia_ffmpeg_context low success ..." << endl;
    }

#endif

    cout << "Bind module start ..." << endl;
    pthread_t venc_pid, rga_pid, low_venc_pid, high_push_pid, low_push_pid, aenc_pid, audio_push_pid;

#if 1
    MPP_CHN_S stViChn;
    MPP_CHN_S stVencChn;
    RV1126_VI_CONTAINER vi_container;
    above_container_func->get_vi_container(0, &vi_container);
    stViChn.enModId = RK_ID_VI;
    stViChn.s32ChnId = vi_container.vi_chn;
    // stViChn.s32DevId = CAMERA_ID;
#endif

#if 1
    RV1126_VENC_CONTAINER venc_container;
    above_container_func->get_venc_container(0, &venc_container);
    stVencChn.enModId = RK_ID_VENC;
    stVencChn.s32ChnId = venc_container.venc_chn;
    // stVencChn.s32DevId = 0;
    ret = RK_MPI_SYS_Bind(&stViChn, &stVencChn);
    if (ret)
    {
        printf("Bind venc chn failed...\n");
        return -1;
    }
    else
    {
        printf("Bind venc chn success...\n");
    }
    ret = pthread_create(&venc_pid, NULL, camera_venc_thread, NULL);
    if (ret)
    {
        printf("create camera_venc_thread failed...\n");
        return -1;
    }
    else
    {
        printf("create camera_venc_thread success...\n");
    }

    ret = pthread_create(&high_push_pid, NULL, ffmpeg_high_video_push_thread, (void *)high_ffmpeg_config);
    if (ret)
    {
        printf("create ffmpeg_high_video_push_thread failed...\n");
        return -1;
    }
    else
    {
        printf("create ffmpeg_high_video_push_thread success...\n");
    }
#endif

#if 0
    MPP_CHN_S stRgaChn;
    RV1126_RGA_CONTAINER rga_container;
    above_container_func->get_rga_container(0, &rga_container);
    stRgaChn.enModId = RK_ID_RGA;
    stRgaChn.s32ChnId = rga_container.rga_chn;
    ret = RK_MPI_SYS_Bind(&stViChn, &stRgaChn);
    if (ret)
    {
        printf("Bind rga chn failed...\n");
        return -1;
    }
    else
    {
        printf("Bind rga chn success...\n");
    }

    ret = pthread_create(&rga_pid, NULL, camera_rga_thread, NULL);
    if (ret)
    {
        printf("create camera_rga_thread failed...\n");
        return -1;
    }
    else
    {
        printf("create camera_rga_thread success...\n");
    }

    ret = pthread_create(&low_venc_pid, NULL, camera_low_venc_thread, NULL);
    if (ret)
    {
        printf("create camera_low_venc_thread failed...\n");
        return -1;
    }
    else
    {
        printf("create camera_low_venc_thread success...\n");
    }

    ret = pthread_create(&low_push_pid, NULL, ffmpeg_low_video_push_thread, (void *)low_ffmpeg_config);
    if (ret)
    {
        printf("create ffmpeg_low_video_push_thread failed...\n");
        return -1;
    }
    else
    {
        printf("create ffmpeg_low_video_push_thread success...\n");
    }

#endif

#if 1
    MPP_CHN_S stAiChn;
    MPP_CHN_S stAencChn;
    RV1126_AI_CONTAINER ai_container;
    RV1126_AENC_CONTAINER aenc_container;
    above_container_func->get_ai_container(0, &ai_container);
    above_container_func->get_aenc_container(0, &aenc_container);
    stAiChn.enModId = RK_ID_AI;
    stAiChn.s32ChnId = ai_container.ai_chn;
    stAencChn.enModId = RK_ID_AENC;
    stAencChn.s32ChnId = aenc_container.aenc_chn;
    ret = RK_MPI_SYS_Bind(&stAiChn, &stAencChn);
    if (ret)
    {
        printf("Bind aenc chn failed...\n");
        return -1;
    }
    else
    {
        printf("Bind aenc chn success...\n");
    }

    ret = pthread_create(&aenc_pid, NULL, mic_aenc_thread, NULL);
    if (ret)
    {
        printf("create mic_aenc_thread failed...\n");
        return -1;
    }
    else
    {
        printf("create mic_aenc_thread success...\n");
    }

    ret = pthread_create(&audio_push_pid, NULL, audio_push_thread, (void *)high_ffmpeg_config);
    if (ret)
    {
        printf("create mic_aenc_thread failed...\n");
        return -1;
    }
    else
    {
        printf("create mic_aenc_thread success...\n");
    }
   
#endif

    return 0;
}
